export const mainMenu = [
    {
        "key": '1',
        "name": '患者管理',
        description: "<%frontName=medicalVisit%>",
        children: [
            {
                name: "患者档案",
                key: "patientManage",
                type: "menu",
                url: "Home",
                description: "<%frontName=patientManage%>",
            },
            {
                name: "就诊登记",
                key: "visitManage",
                type: "menu",
                url: "Home",
                description: "<%frontName=visitManage%>",
            },
        ]
    },
    {
        "key": '2',
        "name": '诊疗管理',
        description: "<%frontName=medicalVisit%>",
        children: [
            {
                name: "医生接诊",
                key: "diagnosis",
                type: "menu",
                url: "Home",
                description: "<%frontName=diagnosis%>",
            },
            {
                name: "医技报告",
                key: "examList",
                type: "menu",
                url: "Home",
                description: "<%frontName=examList%>",
            },
            {
                name: "项目执行",
                key: "noExamList",
                type: "menu",
                url: "Home",
                description: "<%frontName=noExamList%>",
            },
        ]
    },
    {
        "key": '3',
        "name": '基础数据',
        description: "<%frontName=medicalVisit%>",
        children: [
            {
                name: "诊断字典",
                key: "diagnosisDict",
                type: "menu",
                url: "Home",
                description: "<%frontName=diagnosisDict%>",
            },
            {
                name: "医嘱字典",
                key: "orderItemDict",
                type: "menu",
                url: "Home",
                description: "<%frontName=orderItemDict%>",
            }
        ]
    },
    {
        "key": '4',
        "name": '系统管理',
        description: "<%frontName=systemManage%>",
        children: [
            {
                name: "用户管理",
                key: "userManage",
                type: "menu",
                url: "Home",
                description: "<%frontName=userManage%>",
            },
            {
                name: "科室管理",
                key: "departManage",
                type: "menu",
                url: "Home",
                description: "<%frontName=departManage%>",
            },
            {
                name: "角色管理",
                key: "roleManage",
                type: "menu",
                url: "Home",
                description: "<%frontName=roleManage%>",
            },
            {
                name: "操作日志",
                key: "operlog",
                type: "menu",
                url: "Home",
                description: "<%frontName=operlog%>",
            }
        ]
    },
]

export const getMenuList = (keys) => {
    // [
    //     "patientManage",
    //     "visitManage",
    //     "diagnosisDict",
    //     "orderItemDict"
    // ]
    // 递归过滤函数
    const filterMenuTree = (menuItems, permissionKeys) => {
        if(!menuItems || !menuItems.length) return []
        
        return menuItems.reduce((acc, item) => {
            // 复制一份，避免修改原数据
            let newItem = JSON.parse(JSON.stringify(item))
            
            if(item.children && item.children.length) {
                // 递归处理子节点
                newItem.children = filterMenuTree(item.children, permissionKeys)
                // 如果子节点有匹配的,保留父节点
                if(newItem.children.length) {
                    acc.push(newItem)
                }
            } else {
                // 叶子节点直接判断
                if(permissionKeys.includes(item.key)) {
                    acc.push(newItem)
                }
            }
            return acc
        }, [])
    }
    
    return filterMenuTree(mainMenu, keys)
}